﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;
using System.Xml.Linq;
using System.IO;

namespace Ruandao.Misc
{
    public class DynamicXmlNode : DynamicObject
    {
        private XElement _element;

        public DynamicXmlNode(string name)
            : this(new XElement(name))
        {
        }

        public DynamicXmlNode(XElement element)
        {
            _element = element;
        }

        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            string name = binder.Name;

            if (String.CompareOrdinal(name, "SelectAll") == 0)
            {
                IEnumerable<XElement> selectedElements = null;

                if (args.Length == 0)
                {
                    selectedElements = _element.Descendants();
                }
                else
                {
                    selectedElements = _element.Descendants(args[0].ToString());
                }

                result = new DynamicXmlNodeList(selectedElements);
                return true;
            }
            else if (String.CompareOrdinal(name, "SelectChildren") == 0)
            {
                IEnumerable<XElement> selectedElements = null;

                if (args.Length == 0)
                {
                    selectedElements = _element.Elements();
                }
                else
                {
                    selectedElements = _element.Elements(args[0].ToString());
                }

                result = new DynamicXmlNodeList(selectedElements);
                return true;
            }

            return base.TryInvokeMember(binder, args, out result);
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            string name = binder.Name;

            if (String.CompareOrdinal(name, "Name") == 0)
            {
                result = _element.Name.LocalName;
                return true;
            }
            else if (String.CompareOrdinal(name, "Parent") == 0)
            {
                XElement parent = _element.Parent;
                if (parent != null)
                {
                    result = new DynamicXmlNode(parent);
                    return true;
                }
                result = null;
                return true;
            }
            else if (String.CompareOrdinal(name, "Value") == 0)
            {
                result = _element.Value;
                return true;
            }
            else if (String.CompareOrdinal(name, "Nodes") == 0)
            {
                result = new DynamicXmlNodeList(_element.Elements());
                return true;
            }
            else if (String.CompareOrdinal(name, "Xml") == 0)
            {
                StringWriter sw = new StringWriter();
                _element.Save(sw, SaveOptions.None);

                result = sw.ToString();
                return true;
            }
            else
            {
                XAttribute attribute = _element.Attribute(name);
                if (attribute != null)
                {
                    result = attribute.Value;
                    return true;
                }

                var childNodes = _element.Elements(name).ToArray();
                if (childNodes.Length == 1)
                {
                    if (!childNodes[0].HasElements)
                    {
                        result = childNodes[0].Value;
                        return true;
                    }
                    result = new DynamicXmlNode(childNodes[0]);
                    return true;
                }
                else if (childNodes.Length > 1)
                {
                    result = new DynamicXmlNodeList(childNodes);
                    return true;
                }
            }

            if ("ABCDEFGHIJKLMNOPQRSTUVWXYZ".Contains(name[0]))
            {
                return base.TryGetMember(binder, out result);
            }
            result = null;
            return true;
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            string name = binder.Name;

            if (String.CompareOrdinal(name, "Value") == 0)
            {
                _element.Value = (value != null) ? value.ToString() : String.Empty;
            }

            return base.TrySetMember(binder, value);
        }
    }
}
